////////////////////////////////////////////////////////////////////////////////////////////
//GraphManager.h
//
//Copyright 2010 Eric Callahan. All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY ERIC CALLAHAN ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ERIC CALLAHAN OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Eric Callahan.
////////////////////////////////////////////////////////////////////////////////////////////
#pragma once

#include "CodecManager.h"
#include "video.h"

using namespace std;

// Playback Specific constants and enumerations
const long MIN_VOLUME = -10000;
const long MAX_VOLUME = 0;

enum PlaybackState
{
	STATE_RUNNING,
	STATE_PAUSED,
	STATE_STOPPED,
	STATE_CLOSED
};

// callback for parent window responding to graph events
struct GraphEventCallback
{
	virtual void OnGraphEvent(long eventCode, LONG_PTR param1, LONG_PTR param2) = 0;
};


/********************************************************************************
*
*	Class:	GraphManager
*	Desc:	Builds the filter graph for the HDPVR Live Viewer application.  This
*			class manages all directshow filters necessary.  It enumerates
*			supported filters/codecs on the user system, and supplies an interface
*			for the user to select their own codec if necessary.
*
*********************************************************************************/
class GraphManager
{
public:
	GraphManager(HWND hwndVideo, CodecManager* codecMan);
	~GraphManager();

	HRESULT SetEventWindow(HWND hwnd, UINT msg);
	PlaybackState State() const { return m_playState; }

	// builds filter graph
	HRESULT OpenGraph();
	void CloseGraph();
	
	// Streaming
	HRESULT Play();
	HRESULT Stop();

	// EVR functionality
	BOOL    HasVideo() const;
	HRESULT UpdateVideoWindow(const LPRECT prc);
	HRESULT Repaint(HDC hdc);
	HRESULT DisplayModeChanged();

	// events
	HRESULT HandleGraphEvent(GraphEventCallback *pCB);

	// Audio
	HRESULT	Mute(const BOOL& bMute);
	BOOL	IsMuted() const { return m_bMute; }
	HRESULT	SetVolume(const long& lVol);
	long	GetVolume() const { return m_lVolume; }
	
	//****BEGIN USER SELECTION ROUTINES****
	//The following group of functions allow the caller to customize
	//the filters used in the graph
	HRESULT SetAudDec(FilterIdent* fName) 
	{
		m_curAudDec = fName;

		if (m_curAudDec == NULL)
			return E_FAIL;
		else
			return S_OK;
	}

	HRESULT SetVidDec(FilterIdent* fName)
	{
		m_curVidDec = fName;

		if (m_curVidDec == NULL)
			return E_FAIL;
		else
			return S_OK;
	}
	
	HRESULT SetXBar(FilterIdent* fName)
	{
		m_curXBar = fName;

		if (m_curXBar == NULL)
			return E_FAIL;
		else
			return S_OK;
	}

	HRESULT SetCapDev(FilterIdent* fName)
	{
		m_curCapDevice = fName;

		if (m_curCapDevice == NULL)
			return E_FAIL;
		else
			return S_OK;
	}

	HRESULT SetEncoder(FilterIdent* fName)
	{
		m_curEncoder = fName;

		if (m_curEncoder == NULL)
			return E_FAIL;
		else
			return S_OK;
	}
	//****END USER SELECTION ROUTINES****

private:

	HRESULT InitializeGraph();
	void	TearDownGraph();
    HRESULT CreateVideoRenderer();
	HRESULT	RenderStreams(IBaseFilter *pSource);
	HRESULT SetupCaptureGraph();
	HRESULT UpdateVolume();
	HRESULT AddCapDeviceToGraph(IBaseFilter** pCrossBar, IBaseFilter** pCapDevice, IBaseFilter** pEncoder);
	HRESULT ConfigureMPEG2Demux(IBaseFilter** mpeg2Demux);
	HRESULT ConnectFilters(IBaseFilter** outputFilter, IBaseFilter** inputFilter, const UINT& outPinIndex, const UINT& inPinIndex);

	PlaybackState	m_playState;

	HWND			m_hwndVideo;	    // Video clipping window
	HWND			m_hwndEvent;	    // Window to receive events
	UINT			m_eventMsg;			// Windows message for graph events

	// Audio
    BOOL            m_bAudioStream;		// Is there an audio stream?
	long			m_lVolume;			// Current volume (unless muted)
	BOOL			m_bMute;		    // Volume muted?		

	IGraphBuilder	*m_pGraph;
	IMediaControl	*m_pControl;
	IMediaEventEx	*m_pEvent;
	IBasicAudio		*m_pAudio;

	DWORD m_TokenROT;					// Token used when adding graph to ROT

    BaseVideoRenderer   *m_pVideo;

	CodecManager*	m_codecMan;
	
	FilterIdent* m_curAudDec;			// the current user selected audio decoder
	FilterIdent* m_curVidDec;			// the current user selected video decoder
	
	//***Capture Device Specific Filters***
	FilterIdent* m_curXBar;
	FilterIdent* m_curCapDevice;
	FilterIdent* m_curEncoder;
	
};

HRESULT RemoveUnconnectedRenderer(IGraphBuilder *pGraph, IBaseFilter *pRenderer, BOOL *pbRemoved);